package com.example.compose.retrofit.crash

import android.content.ClipData
import android.content.ClipboardManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color

class LogcatActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val crash = intent.getStringExtra("crash") ?: ""
        setContent {
            val scrollState = rememberScrollState()
            Text(
                text = crash,
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(scrollState)
                    .background(Color(0xFF919191))
                    .clickable { copyStr(crash) },
                color = Color(0xFFFFFFFF)
            )
        }
    }

    /**
     * 复制内容到剪切板
     *
     * @param copyStr
     * @return
     */
    private fun copyStr(copyStr: String): Boolean {
        return try {
            val cm: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            val mClipData = ClipData.newPlainText("Label", copyStr)
            cm.setPrimaryClip(mClipData)
            Toast.makeText(this,"已经复制到粘贴板",Toast.LENGTH_SHORT).show()
            true
        } catch (e: Exception) {
            false
        }
    }

}
